import tkinter as tk
from tkinter import ttk
from tkinter import filedialog


class FolderSelector(ttk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.pack(fill="x")

        self.label = ttk.Label(self, text="请输入文件夹地址：")
        self.label.pack(side="left")

        self.entry = ttk.Entry(self)
        self.entry.pack(side="left", fill="x", expand=True)

        self.button = ttk.Button(self, text="选择文件夹", command=self.select_folder)
        self.button.pack(side="left")

    def set_value(self, folder: str):
        self.entry.delete(0, tk.END)
        self.entry.insert(0, folder)

    def select_folder(self):
        folder_path = filedialog.askdirectory(initialdir=self.entry.get())
        if folder_path:
            self.entry.delete(0, tk.END)
            self.entry.insert(0, folder_path)

    @property
    def value(self) -> str:
        return self.entry.get()


if __name__ == "__main__":
    root = tk.Tk()
    app = FolderSelector(root)
    root.mainloop()
